<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import type { FormInstance, FormRules } from 'element-plus';
import type { Group } from '@/apis/modules/mock/group/type';
import { mock_API } from '@/apis';
import useTestCalendarStore from '@/stores/modules/testCalendar';

import * as d3 from 'd3';
const timeFormat = d3.timeFormat('%Y-%m-%d');
const testCalendarStore = useTestCalendarStore();

const { getGroupList, addEvent } = mock_API;
interface RuleForm {
  id?: string;
  title: string;
  allDay?: boolean;
  startStr: string;
  endStr: string;
  groupId: string;
  desc?: string;
  bgColor: string;
}

const ruleFormRef = ref<FormInstance>();

const ruleForm = reactive<RuleForm>({
  title: '',
  allDay: false,
  startStr: '',
  endStr: '',
  groupId: '',
  desc: '',
  bgColor: '',
});

let groups = reactive<Group[]>([]);

const isLoading = ref(false);

const rules = reactive<FormRules<RuleForm>>({
  title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
  startStr: [
    {
      required: true,
      message: '请选择开始时间',
      trigger: 'change',
    },
  ],
  endStr: [
    {
      required: true,
      message: '请选择结束时间',
      trigger: 'change',
    },
  ],
  groupId: [{ required: true, message: '请选择所属组', trigger: 'blur' }],
});

onMounted(async () => {
  groups = await getGroupList();
});

const handleSelectGroup = (value: any) => {
  console.log(value);
  ruleForm.bgColor = groups.find((item) => item.id == value)!.color;
};
// 提交按钮
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  isLoading.value = true;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      console.log('submit!');
      // setTimeout(async () => {
      const res = await addEvent(ruleForm);
      console.log(res);
      testCalendarStore.addEvent({
        id: res.id,
        title: res.title,
        backgroundColor: res.bgColor,
        start: timeFormat(new Date(res.startStr)),
        end: timeFormat(new Date(res.endStr)),
        groupId: res.groupId,
        meta: { ...res },
      });
      // 结束加载状态
      isLoading.value = false;
      // 重置表单
      formEl.resetFields();
      // 关闭抽屉
      testCalendarStore.drawerIsShow = false;
      // }, 3000);
    } else {
      isLoading.value = false;
      console.log('error submit!', fields);
    }
  });
};
// 重置按钮
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

<script lang="ts">
export default {
  name: 'EventForm',
};
</script>

<template>
  <section>
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="100px"
      label-position="left"
      status-icon
    >
      <el-form-item label="标题" prop="title">
        <el-input v-model="ruleForm.title" />
      </el-form-item>

      <el-form-item label="开始时间" prop="startStr">
        <el-date-picker
          v-model="ruleForm.startStr"
          type="datetime"
          placeholder="请选择开始时间"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>

      <el-form-item label="结束时间" prop="endStr">
        <el-date-picker
          v-model="ruleForm.endStr"
          type="datetime"
          placeholder="请选择结束时间"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>

      <el-form-item label="所属组" prop="groupId">
        <el-select
          v-model="ruleForm.groupId"
          placeholder="请选择所属组"
          @change="handleSelectGroup"
        >
          <el-option
            v-for="group in groups"
            :key="group.id"
            :label="group.name"
            :value="group.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="是否为全天" prop="allDay">
        <el-switch v-model="ruleForm.allDay" />
      </el-form-item>

      <el-form-item>
        <div class="flex flex-1 items-center justify-between">
          <el-button type="primary" @click="submitForm(ruleFormRef)" :loading="isLoading">
            创建
          </el-button>
          <el-button @click="resetForm(ruleFormRef)">重置</el-button>
        </div>
      </el-form-item>
    </el-form>
  </section>
</template>

<style scoped>
/* PC电脑端 */
@media screen and (width <= 1024px) {
}
</style>
